//
//  AttractionsListViewController.m
//  HZHome
//
//  Created by SunShine on 13-10-13.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "AttractionsListViewController.h"
#import "AttractionItem.h"
#import "AttractionsView.h"
#import "UIImageView+WebCache.h"
#import "TravelViewController.h"

#define ITEM_MARGINS 10.0
#define ITEM_SPACES 20.0
#define ITEM_WIDTH 140.0
#define ITEM_HEIGHT 170.0

@interface AttractionsListViewController ()

@end

@implementation AttractionsListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"海珠美景";
    [self startLoadingView];
    _dataService = [[AttractionsListService alloc]init];
    _dataService.delegate = self;
    [_dataService requestList];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Data Service Delegate Method
-(void)loadDataFinished
{
    for (int i = 0; i < _dataService.dataList.count; i++) {
        AttractionItem *item = [_dataService.dataList objectAtIndex:i];
        CGFloat itemX = ITEM_MARGINS + i%2*(ITEM_WIDTH+ITEM_SPACES);
        CGFloat itemY = ITEM_MARGINS + i/2*(ITEM_HEIGHT+ITEM_SPACES);
        CGRect itemRect = CGRectMake(itemX, itemY, ITEM_WIDTH, ITEM_HEIGHT);
        AttractionsView *itemView = [[[NSBundle mainBundle]loadNibNamed:@"AttractionsView" owner:self options:nil] objectAtIndex:0];
        itemView.frame = itemRect;
        itemView.titleLabel.text = item.title;
        itemView.tag = i;
        [itemView addTarget:self action:@selector(itemTouched:) forControlEvents:UIControlEventTouchUpInside];
        //图片
        __block UIActivityIndicatorView *activityIndicator;
        __weak UIImageView *weakImageView = itemView.imageView;
        [itemView.imageView setImageWithURL:[NSURL URLWithString:item.picture] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize)
        {
            if (!activityIndicator)
            {
                [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                activityIndicator.center = weakImageView.center;
                [activityIndicator startAnimating];
            }
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
        {
            [activityIndicator removeFromSuperview];
            activityIndicator = nil;
        }];
        [self.scrollView addSubview:itemView];
    }
    //控制scroll content的高度
    int numOfLines = ceil([_dataService.dataList count]/2.0);
    CGFloat totalHeight = ITEM_MARGINS*2+numOfLines*ITEM_HEIGHT+(numOfLines-1)*ITEM_SPACES;
    self.scrollView.contentSize = CGSizeMake(320.0, totalHeight);
    [self stopLoadingView];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
}

#pragma mark - Touch Event Method
-(void)itemTouched:(id)sender
{
    AttractionsView *view = (AttractionsView *)sender;
    
    AttractionItem *item = [_dataService.dataList objectAtIndex:view.tag];
    TravelViewController *travelVC = [[TravelViewController alloc]initWithNibName:@"TravelViewController" bundle:nil];
    travelVC.dataItem = item;
    [self.navigationController pushViewController:travelVC animated:YES];
}
@end
